#include "hwboard.h"
#include <stm32_def.h>
#ifdef PCF8563
Rtc_Pcf8563 pcf8563(PB7, PB6);
#else
STM32RTC &rtc = STM32RTC::getInstance();
#endif
RS485Class serial2(PA2, PA3, PF9, -1);
SPIClass fspi(PB15, PB14, PB13);

FlashFs flash_fs;
RS485Class serial3(PB10, PB11, PF8, -1);
RS485Class serial4(PC10, PC11, -1, -1);
RS485Class serial5(PC12, PD2, PD0, -1);
const uint8_t pin_beep = PA8;
#ifdef BUILD_NUMBER
const int build = BUILD_NUMBER;
#else
const int build = 0;
#endif
unsigned int uptime;
int atemp_init, atemp, avref;
IOVAR io;
uint16_t year, wdg_time;
uint8_t rtc_write, month, day, hour, minute, second, week, wdg_en, status = 1;
KC1110::KC1110()
{
    extern int VECTOR_ISR;
    SCB->VTOR = (uint32_t)&VECTOR_ISR;
}
void KC1110::baseinit()
{
    pinMode(pin_beep, OUTPUT);
    digitalWrite(pin_beep, HIGH);
#ifdef PCF8563
    pcf8563.begin();
#else
    rtc.begin();
#endif
    flash_fs.begin(&fspi, PD10);
    flash_fs.dir();
}

void KC1110::begin()
{

    io_init();

    // atemp_init = analogRead(ATEMP);
    pt100.begin();
    digitalWrite(pin_beep, LOW);
};
void KC1110::io_init()
{
    analogReadResolution(12);
    analogWriteResolution(12);
    pinMode(XA1, INPUT_ANALOG);
    pinMode(YA1, OUTPUT);
    pinMode(YA2, OUTPUT);
    pinMode(X1, INPUT_PULLUP);
    pinMode(X2, INPUT_PULLUP);
    pinMode(X3, INPUT_PULLUP);
    pinMode(X4, INPUT_PULLUP);
    pinMode(Y1, OUTPUT);
    pinMode(Y2, OUTPUT);
    pinMode(Y3, OUTPUT);
    pinMode(Y4, OUTPUT);
    pinMode(Y5, OUTPUT);
    pinMode(Y6, OUTPUT);
}

void KC1110::input()
{
    pt100.loop();
    io.xa1 = analogRead(XA1);
    io.xx1 = digitalRead(X1) ? 0 : 1;
    io.xx2 = digitalRead(X2) ? 0 : 1;
    io.xx3 = digitalRead(X3) ? 0 : 1;
    io.xx4 = digitalRead(X4) ? 0 : 1;
    io.xt1 = pt100.rvalue();
    static long last_tick;
    if (millis() - last_tick >= 1000)
    {
#ifdef PCF8563
        pcf8563.getDateTime();
        year = pcf8563.getYear() + (pcf8563.getCentury() ? 1900 : 2000);
        month = pcf8563.getMonth();
        day = pcf8563.getDay();
        week = pcf8563.getWeekday();
        hour = pcf8563.getHour();
        minute = pcf8563.getMinute();
        second = pcf8563.getSecond();
#else
        year = rtc.getYear() + 2000;
        month = rtc.getMonth();
        day = rtc.getDay();
        week = rtc.getWeekDay();
        hour = rtc.getHours();
        minute = rtc.getMinutes();
        second = rtc.getSeconds();
#endif
        last_tick = millis();
    }
}
void KC1110::output()
{
    digitalWrite(Y1, io.yy1 ? 0 : 1);
    digitalWrite(Y2, io.yy2 ? 0 : 1);
    digitalWrite(Y3, io.yy3 ? 0 : 1);
    digitalWrite(Y4, io.yy4 ? 0 : 1);
    digitalWrite(Y5, io.yy5 ? 0 : 1);
    digitalWrite(Y6, io.yy6 ? 0 : 1);
    analogWrite(YA1, io.ya1);
    analogWrite(YA2, io.ya2);
    if (rtc_write)
    {
#ifdef PCF8563
        pcf8563.setDateTime(day, week, month, 0, year - 2000, hour, minute, second);
#else
        rtc.setDate(day, month, year - 2000);
        rtc.setTime(hour, minute, second);
#endif
        rtc_write = 0;
    }
}
KC1110 board;
const char *hwModel = "KC1110-2";
pt100_hx711 pt100(PE14, PE15);
